package com.yogee.topseller.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.bean.RemitCalendarBean;

import java.util.List;
import java.util.Map;

/**
 * 项目名称：TopSeller
 * 类描述：
 * 创建时间：2017/7/10 0010 9:14
 * 创建人：wangye
 * 修改备注：
 */

public class PayCalendarPgAdapter extends PagerAdapter {
    private Context context;
    private dateClick dateClick;
    private Map<Integer, List<RemitCalendarBean.CalendarListBean>> map;
    private Map<Integer, List<String>> seleMap;
    private int mChildCount = 0;
    private int nowPos, nowDate;

    public void setDateClick(PayCalendarPgAdapter.dateClick dateClick) {
        this.dateClick = dateClick;
    }

    public PayCalendarPgAdapter(Context context) {
        this.context = context;
    }

    public void setNowBg(int nowPos,int nowDate){
        this.nowPos = nowPos;
        this.nowDate = nowDate;
        notifyDataSetChanged();
    }

    public void addData(Map<Integer, List<RemitCalendarBean.CalendarListBean>> map, Map<Integer, List<String>> seleMap) {
        this.map = map;
        this.seleMap = seleMap;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return map == null ? 0 : map.size();
    }

    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        if (mChildCount > 0) {
            mChildCount--;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) { // 这个方法用来实例化页卡

        View view = LayoutInflater.from(context).inflate(R.layout.item_pay_calenddar, null);

        final List<RemitCalendarBean.CalendarListBean> bean = map.get(position);
        final List<String> seles = seleMap.get(position);

        final TextView date1 = (TextView) view.findViewById(R.id.date1);
        final TextView date2 = (TextView) view.findViewById(R.id.date2);
        final TextView date3 = (TextView) view.findViewById(R.id.date3);
        final TextView date4 = (TextView) view.findViewById(R.id.date4);
        final TextView date5 = (TextView) view.findViewById(R.id.date5);
        final TextView date6 = (TextView) view.findViewById(R.id.date6);
        final TextView date7 = (TextView) view.findViewById(R.id.date7);

        String[] sourceStrArray = bean.get(0).getReturnDate().split("-");
        String[] sourceStrArray1 = bean.get(1).getReturnDate().split("-");
        String[] sourceStrArray2 = bean.get(2).getReturnDate().split("-");
        String[] sourceStrArray3 = bean.get(3).getReturnDate().split("-");
        String[] sourceStrArray4 = bean.get(4).getReturnDate().split("-");
        String[] sourceStrArray5 = bean.get(5).getReturnDate().split("-");
        String[] sourceStrArray6 = bean.get(6).getReturnDate().split("-");
        date1.setText(sourceStrArray[2]);
        date2.setText(sourceStrArray1[2]);
        date3.setText(sourceStrArray2[2]);
        date4.setText(sourceStrArray3[2]);
        date5.setText(sourceStrArray4[2]);
        date6.setText(sourceStrArray5[2]);
        date7.setText(sourceStrArray6[2]);

        final TextView num1 = (TextView) view.findViewById(R.id.num1);
        final TextView num2 = (TextView) view.findViewById(R.id.num2);
        final TextView num3 = (TextView) view.findViewById(R.id.num3);
        final TextView num4 = (TextView) view.findViewById(R.id.num4);
        final TextView num5 = (TextView) view.findViewById(R.id.num5);
        final TextView num6 = (TextView) view.findViewById(R.id.num6);
        final TextView num7 = (TextView) view.findViewById(R.id.num7);
        num1.setText(bean.get(0).getNum());
        num2.setText(bean.get(1).getNum());
        num3.setText(bean.get(2).getNum());
        num4.setText(bean.get(3).getNum());
        num5.setText(bean.get(4).getNum());
        num6.setText(bean.get(5).getNum());
        num7.setText(bean.get(6).getNum());

        final LinearLayout layout1 = (LinearLayout) view.findViewById(R.id.layout1);
        final LinearLayout layout2 = (LinearLayout) view.findViewById(R.id.layout2);
        final LinearLayout layout3 = (LinearLayout) view.findViewById(R.id.layout3);
        final LinearLayout layout4 = (LinearLayout) view.findViewById(R.id.layout4);
        final LinearLayout layout5 = (LinearLayout) view.findViewById(R.id.layout5);
        final LinearLayout layout6 = (LinearLayout) view.findViewById(R.id.layout6);
        final LinearLayout layout7 = (LinearLayout) view.findViewById(R.id.layout7);

        for (int i = 0; i < seles.size(); i++) {
            if (seles.get(i).equals("1")) {
                switch (i) {
                    case 0:
                        date1.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                        layout1.setBackgroundResource(R.mipmap.circle_black);
                        break;
                    case 1:
                        date2.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                        layout2.setBackgroundResource(R.mipmap.circle_black);
                        break;
                    case 2:
                        date3.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                        layout3.setBackgroundResource(R.mipmap.circle_black);
                        break;
                    case 3:
                        date4.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                        layout4.setBackgroundResource(R.mipmap.circle_black);
                        break;
                    case 4:
                        date5.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                        layout5.setBackgroundResource(R.mipmap.circle_black);
                        break;
                    case 5:
                        date6.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                        layout6.setBackgroundResource(R.mipmap.circle_black);
                        break;
                    case 6:
                        date7.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                        layout7.setBackgroundResource(R.mipmap.circle_black);
                        break;
                }
            }
            if (seles.get(i).equals("2")) {
                switch (i) {
                    case 0:
                        changeStatus(layout1, date1, num1);
                        break;
                    case 1:
                        changeStatus(layout2, date2, num2);
                        break;
                    case 2:
                        changeStatus(layout3, date3, num3);
                        break;
                    case 3:
                        changeStatus(layout4, date4, num4);
                        break;
                    case 4:
                        changeStatus(layout5, date5, num5);
                        break;
                    case 5:
                        changeStatus(layout6, date6, num6);
                        break;
                    case 6:
                        changeStatus(layout7, date7, num7);
                        break;
                }
            }
        }
        if (nowPos == position){
            switch (nowDate){
                case 0:
                    changeStatus(layout1, date1, num1);
                    break;
                case 1:
                    changeStatus(layout2, date2, num2);
                    break;
                case 2:
                    changeStatus(layout3, date3, num3);
                    break;
                case 3:
                    changeStatus(layout4, date4, num4);
                    break;
                case 4:
                    changeStatus(layout5, date5, num5);
                    break;
                case 5:
                    changeStatus(layout6, date6, num6);
                    break;
                case 6:
                    changeStatus(layout7, date7, num7);
                    break;
            }
        }

        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < seleMap.size(); i++) {
                    for (int j = 0; j < seleMap.get(i).size(); j++) {
                        if (!seleMap.get(i).get(j).equals("1")) {//今天不是选中
                            if (i == position && j == 0) {
                                seleMap.get(i).set(j, "2");
                                nowPos = 5000;
                            } else {
                                seleMap.get(i).set(j, "0");
                            }
                        } else {
                            if (i == position && j == 0) {
                                nowPos = i;
                                nowDate = 0;
                            }
                        }
                    }
                }
                notifyDataSetChanged();
                dateClick.click(bean.get(0).getReturnDate(),bean.get(0).getTopDate());
            }
        });
        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < seleMap.size(); i++) {
                    for (int j = 0; j < seleMap.get(i).size(); j++) {
                        if (!seleMap.get(i).get(j).equals("1")) {
                            if (i == position && j == 1) {
                                seleMap.get(i).set(j, "2");
                                nowPos = 5000;
                            } else {
                                seleMap.get(i).set(j, "0");
                            }
                        }else {
                            if (i == position && j == 1) {
                                nowPos = i;
                                nowDate = 1;
                            }
                        }
                    }
                }
                notifyDataSetChanged();
                dateClick.click(bean.get(1).getReturnDate(),bean.get(1).getTopDate());
            }
        });
        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < seleMap.size(); i++) {
                    for (int j = 0; j < seleMap.get(i).size(); j++) {
                        if (!seleMap.get(i).get(j).equals("1")) {
                            if (i == position && j == 2) {
                                seleMap.get(i).set(j, "2");
                                nowPos = 5000;
                            } else {
                                seleMap.get(i).set(j, "0");
                            }
                        }else {
                            if (i == position && j == 2) {
                                nowPos = i;
                                nowDate = 2;
                            }
                        }
                    }
                }
                notifyDataSetChanged();
                dateClick.click(bean.get(2).getReturnDate(),bean.get(2).getTopDate());
            }
        });
        layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < seleMap.size(); i++) {
                    for (int j = 0; j < seleMap.get(i).size(); j++) {
                        if (!seleMap.get(i).get(j).equals("1")) {
                            if (i == position && j == 3) {
                                seleMap.get(i).set(j, "2");
                                nowPos = 5000;
                            } else {
                                seleMap.get(i).set(j, "0");
                            }
                        }else {
                            if (i == position && j == 3) {
                                nowPos = i;
                                nowDate = 3;
                            }
                        }
                    }
                }
                notifyDataSetChanged();
                dateClick.click(bean.get(3).getReturnDate(),bean.get(3).getTopDate());
            }
        });
        layout5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < seleMap.size(); i++) {
                    for (int j = 0; j < seleMap.get(i).size(); j++) {
                        if (!seleMap.get(i).get(j).equals("1")) {
                            if (i == position && j == 4) {
                                seleMap.get(i).set(j, "2");
                                nowPos = 5000;
                            } else {
                                seleMap.get(i).set(j, "0");
                            }
                        }else {
                            if (i == position && j == 4) {
                                nowPos = i;
                                nowDate = 4;
                            }
                        }
                    }
                }
                notifyDataSetChanged();
                dateClick.click(bean.get(4).getReturnDate(),bean.get(4).getTopDate());
            }
        });
        layout6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < seleMap.size(); i++) {
                    for (int j = 0; j < seleMap.get(i).size(); j++) {
                        if (!seleMap.get(i).get(j).equals("1")) {
                            if (i == position && j == 5) {
                                seleMap.get(i).set(j, "2");
                                nowPos = 5000;
                            } else {
                                seleMap.get(i).set(j, "0");
                            }
                        }else {
                            if (i == position && j == 5) {
                                nowPos = i;
                                nowDate = 5;
                            }
                        }
                    }
                }
                notifyDataSetChanged();
                dateClick.click(bean.get(5).getReturnDate(),bean.get(5).getTopDate());
            }
        });
        layout7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < seleMap.size(); i++) {
                    for (int j = 0; j < seleMap.get(i).size(); j++) {
                        if (!seleMap.get(i).get(j).equals("1")) {
                            if (i == position && j == 6) {
                                seleMap.get(i).set(j, "2");
                                nowPos = 5000;
                            } else {
                                seleMap.get(i).set(j, "0");
                            }
                        }else {
                            if (i == position && j == 6) {
                                nowPos = i;
                                nowDate = 6;
                            }
                        }
                    }
                }
                notifyDataSetChanged();
                dateClick.click(bean.get(6).getReturnDate(),bean.get(6).getTopDate());
            }
        });

        container.addView(view);
        return view;
    }
 
    public interface dateClick {
        void click(String date,String topDate);
    }

    private void changeStatus(LinearLayout layout, TextView textDate, TextView textNum) {
        layout.setBackgroundResource(R.mipmap.circle_red);
        textDate.setTextColor(0xffffffff);
        textNum.setTextColor(0xffffffff);
    }

}

